import 'bootstrap/dist/css/bootstrap.css';
import '../css/index2.scss';


require('bootstrap/dist/js/bootstrap.js')
require('./common.js');
$('#sidebar-menu a[href="key_words.html"]').addClass('active')
$('#sidebar-menu a[href="key_words.html"]').parent('li').addClass('active');


import { keywordsDetailUrl } from './urlconfig/config.js';

import {chartData} from './lineConfig.js'; //获取多站的线条配置

import {getData, chartError, removeChartError} from './chartFunction.js'; //获取ajax请求返回的结果

require('./echartsFunction/chartClass.js');


require('./echartsFunction/tool.js');

let keywords_name = tool.getQueryString('key_word');


// 设置标题
$(".page-title").html(keywords_name + '整体排名趋势图');	
var resDataObj = {
			timeData: [],
			seriesData: {}
		};
		
		var reqOption = {
			data: {
					time: 30,
		        	page_number: 'all',
		        	peywords_name: keywords_name
				},
			url: keywordsDetailUrl,
			type: 'post'		
		}
var chart;	


function creat() {
	getData(reqOption).then(function(res){
		
		if(res.hasOwnProperty('items') || res.items.length > 0) {
			removeChartError($("#chart-content"));
			formatResData(res.items);
			chart = new ChartClass($("#chart-content")[0], chartData);
			chart.init();
		}else {
			removeChartError($("#chart-content"));
			chartError($("#chart-content"))
		}
		
	}, function() {
		removeChartError($("#chart-content"));
		chartError($("#chart-content"))
	})
}
creat();		
function formatResData(resArr) {
	resDataObj.seriesData = {};
	resDataObj.timeData = [];
	let length = resArr.length;
	let webName = [];
	let xAxisTime = [];
	
	for(let index = 0; index < length; index++) {
		webName[index] = resArr[index].name;
		let obj = {};
		let i = 0, j = 0;
		let seriesData = [];
		let dataValue = resArr[index].data;
		for(var p in dataValue) {
			xAxisTime[j++] = tool.formatDate(parseInt(p));
			seriesData[i++] = dataValue[p];
			resDataObj.seriesData[resArr[index].name] = seriesData;
			
		}
		

	}
	resDataObj.timeData = xAxisTime;
	chartData.name = webName;
	chartData.data = resDataObj;
}




// 时间选择
function setTime(dagnumber, ele) {
	reqOption.data.time = dagnumber;
	let ele_siblins = tool.sibling(ele);
	for(let index = 0, length = ele_siblins.length; index < length; index++) {
		tool.removeClass(ele_siblins[index], 'btn-default');
	}
	tool.addClass(ele, 'btn-default');
	chartData.day = dagnumber;
	creat()
	
}



window.setTime = setTime;

